<?php

namespace app\api\server;

//成都正太调用服务类
use think\facade\Db;
use think\Request;

class Zt
{
    public $param;
    public $url;
    public $appKey;
    public $appSecret;
    public $templateId=[
        "YG2024082701"=>5,
        "GR2024082701"=>5,
        "HL2024082701"=>3,
        "JZ2024082701"=>4,
        "ZL2024082701"=>6,
    ];

    public function __construct()
    {
        // 控制器初始化
        $this->_initialize();

    }
    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {
        $env=env("SURVEY_ZT.env");
        $this->url= $env == "formal" ? env("SURVEY_ZT.formal") : env("SURVEY_ZT.test");
        $this->appKey = env("SURVEY_ZT.appKey");
        $this->appSecret = env("SURVEY_ZT.appSecret");
    }


    //创建订单
    public function initiate($orderInfo)
    {
        $param=json_decode($orderInfo["param"],1);
        $param["templateId"]=$this->templateId[$orderInfo["product_code"]];
//        var_dump($param);
        $param=(new HelperZt() )->encrypt(json_encode($param,320));
//        var_dump($param);
        $url=$this->url."/core/api/report/initiate";
//        var_dump($url);
        $return=$this->postRequest($url,$param);
//        var_dump($return);
        //请求接口
        $thirdOrderInfo=[
            "order_no"=>$orderInfo["order_no"],
            "third_order_no"=>$return["data"],
            "param"=>$orderInfo["param"],
            "channel"=>"SURVEY_ZT",
            "industry"=>20, //10学历验证 20个人 30婚恋 40家政 50租赁
            "create_time"=>date("Y-m-d H:i:s")
        ];
        if($return["data"])
        {
            Db::table("zl_survey_report")->insertGetId($thirdOrderInfo);
        }
        return $thirdOrderInfo;
    }


    //查询订单
    public function report($third_order_no)
    {
        $url=$this->url."/core/api/report/inquire/".$third_order_no;
        $thirdOrderInfo=$this->getRequest($url);
        return $thirdOrderInfo;
    }

    //概要查询
    public function outline()
    {
        $url=$this->url."/core/api/report/outline/";
        $param=["name"=>"张三","mobile"=>"13488889999"];
        dump(json_encode($param,320));
        $param=(new HelperZt() )->encrypt(json_encode($param,320));
        dump($param);
        $return = $this->postRequest($url,$param);
        return $return;
    }

    //网络post请求
    public function postRequest($url,$data=[])
    {
        $headers = array("Content-Type: text/plain","appkey:".$this->appKey);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        curl_close($curl);
//        var_dump($output);
        $output=json_decode($output,1);
        return $output;
    }



    public function getRequest($url)
    {
        $headers = array("Content-Type: text/plain","appkey: ".$this->appKey);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        curl_close($curl);
        $output=json_decode($output,1);
        return $output;
    }






}